<!--
 * @Author: Dorange.Wang
 * @Date: 2021-04-30 10:36:10
 * @LastEditors: your name
 * @LastEditTime: 2021-08-24 11:22:01
 * @Description: file content
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="600px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{ isNew ? $t("sys.new_role") : $t("sys.edit_role") }}</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>

    <el-form class="pl30 pr30" :model="formData" ref="form" label-width="120px">
      <el-form-item
        :label="$t('sys.role_name')"
        prop="name"
        :rules="[
          { required: true, message: $t('sys.role_name_not_null'), trigger: 'change' },
        ]"
      >
        <el-input
          v-model="formData.name"
          :placeholder="$t('sys.select_role_name')"
          :maxlength="64"
          show-word-limit
        />
      </el-form-item>

      <el-form-item
        label="状态"
        prop="status"
        :rules="[{ required: true, message: $t('sys.select_status'), trigger: 'change' }]"
      >
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="item in ROLE_STATUS_ENUM"
            :key="item.key"
            :label="item.key"
            >{{ $t('sys.'+ item.i18nLable) }}</el-radio
          >
        </el-radio-group>
      </el-form-item>

      <el-form-item :label="$t('sys.remarks')">
        <el-input
          type="textarea"
          v-model="formData.comment"
          :placeholder="$t('sys.enter_remarks')"
          :maxlength="120"
          show-word-limit
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">{{$t('sys.cancel')}}</el-button>
      <el-button type="primary" @click="submit">{{$t('sys.confirm')}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { ROLE_STATUS_ENUM } from "@/utils/enum";
import { putHmrole, postHmrole } from "@/apis/paas/HmRoleController.js";
const defaultFormData = {
  status: ROLE_STATUS_ENUM[0].key,
};

export default {
  props: {
    folders: {
      type: Array,
      default: () => [],
    },
  },

  data() {
    return {
      showVisible: false,
      ROLE_STATUS_ENUM,
      formData: {
        ...defaultFormData,
      },
      // formData: {},
      // callback: null
    };
  },

  computed: {
    isNew() {
      return !this.formData.id;
    },
  },

  methods: {
    addForm(record = {}) {
      this.showVisible = true;
      this.formData = {
        ...defaultFormData,
        ...record,
      };
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },

    editForm(record = {}) {
      this.showVisible = true;
      this.formData = {
        ...record,
      };
    },

    async submit() {
      try {
        await this.$refs.form.validate();
        const { id, name, status, comment } = this.formData;
        let params = {};
        let request = null;
        if (this.isNew) {
          params = { name, status, comment };
          request = postHmrole;
        } else {
          params = { id, name, status, comment };
          request = putHmrole;
        }
        for (const key in params) {
          params[key] =
            typeof params[key] === "string" ? params[key].trim() : params[key];
        }
        request({
          role: params,
        }).then((res) => {
          this.showVisible = false;
          this.$emit("ok");
        });
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
